/*
 * @Author: yangrongxin
 * @Date: 2021-08-02 17:48:49
 * @LastEditors: yangrongxin
 * @LastEditTime: 2021-09-09 09:44:21
 */
import { useState, useRef } from 'react';
import { View, ScrollView } from '@tarojs/components';
import { EvaluationClass, NavBarSearch, ScrollContent, OrderComponent } from '@/components/index';
import { getMeasureTable } from './service';
import { EvaluationItem } from './components/';
import styles from './index.module.less';

const Evaluation = () => {
  const [curId, setCurId] = useState<string>('');

  const [sort, setSort] = useState<any>({
    orderBy: 'xCreateTime', // 默认按照最新进行排序
    asc: true,
  });

  const [refresherTrigger, setRefresherTrigger] = useState<boolean>(false);

  const contentRef = useRef<any>();
  const orderComponentRef = useRef<any>();
  const classEleRef = useRef<any>();

  return (
    <ScrollView
      scrollWithAnimation
      scrollY
      refresherEnabled
      onRefresherRefresh={() => {
        setRefresherTrigger(true);
        classEleRef?.current?.reload();
        contentRef?.current?.reload(
          {
            measureTableCategoryId: curId,
          },
          true,
        );
      }}
      refresherTriggered={refresherTrigger}
    >
      <View className="hxtPublicWapper">
        {/* 公共组件 搜索测评以及宣教信息 */}
        <NavBarSearch currentFrom={1} />
        {/* 分类 */}
        <EvaluationClass
          ref={classEleRef}
          onChange={(curFnId: string) => {
            contentRef?.current?.reload(
              {
                measureTableCategoryId: curFnId,
              },
              true,
            );
            setCurId(curFnId);
            orderComponentRef?.current?.resetSort();
          }}
        />
        {/* 展示排序的控件 */}
        <OrderComponent
          ref={orderComponentRef}
          onChange={(sortObj: any) => {
            contentRef?.current?.reload(
              {
                measureTableCategoryId: curId,
                ...sortObj,
              },
              true,
            );
            setSort(sortObj);
          }}
        />
        {/* 展示评测内容 */}
        <ScrollContent
          enabledRefresh={false}
          ref={contentRef}
          request={async (data) => getMeasureTable(data)}
          params={{
            measureTableCategoryId: curId,
            ...sort,
          }}
          extendScrollContentWapper={styles.extendScrollContentClass}
          ScrollItem={EvaluationItem}
          onDataFetch={() => setTimeout(() => setRefresherTrigger(false), 200)}
        />
      </View>
    </ScrollView>
  );
};

export default Evaluation;
